home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / fig2mfpic / mfpic.tex < prev    next >
Text File  |  1993-10-10  |  12KB  |  423 lines

  1. %%%
  2. %%%  File: mfpic.tex
  3. %%%  Version:  0.25
  4. %%%
  5. %%%  Principal Author:  Thomas Leathrum
  6. %%%  Assistants:  Bryan Green, Geoffrey Tobin
  7. %%%
  8.  
  9. % preliminary version of macros
  10. % save catcode, as Michael Downes of AMS recommends.
  11. \chardef\oldatcatcode=\catcode`@
  12. \catcode`@=11 % letter
  13. \def\@wout{\immediate\write16\relax}
  14. \@wout {}
  15. \@wout
  16.   {mfpic version 0.25 - 19:20 GMT +10:00 Wed 13 Oct 93}
  17. \@wout
  18.   {fixes for TFM, LaTeX (picture), fractions, catcode, sharp.}
  19. \@wout
  20.   {tests for bad label options, mf char codes out of range (0 - 255).}
  21. \@wout
  22.   {localised short-range declarations.}
  23. \@wout {}
  24. % test whether mfpic is used in LaTeX.
  25. \newif\if@inlatex
  26. % idea from _The TeXbook_ (1986), p. 384:
  27. % \@mfundefined must remain undefined.
  28. \ifx\picture\@mfundefined
  29.   \@inlatexfalse
  30.   \@wout {MFPIC: not in latex}
  31. \else
  32.   \@inlatextrue
  33.   \@wout {MFPIC: in latex}
  34. \fi
  35. \@wout {}
  36. % local declarations
  37. \wlog {MFPIC: local declarations}
  38. \newwrite\@outx
  39. \newbox\@labeledgraph
  40. \newdimen\@graphwd
  41. \newcount\@code
  42. % dimension parameters
  43. \wlog {}
  44. \wlog {MFPIC: dimension parameters}
  45. \newdimen\mfpicunit
  46. \newdimen\pointsize
  47. \newdimen\shadespace
  48. \newdimen\headlen
  49. \newdimen\axisheadlen
  50. \newdimen\hashlen
  51. \newdimen\dashlen
  52. \newdimen\dashspace
  53. % default dimension settings
  54. \mfpicunit=1pt
  55. \pointsize=2pt
  56. \shadespace=1pt
  57. \headlen=3pt
  58. \axisheadlen=5pt
  59. \hashlen=4pt
  60. \dashlen=4pt
  61. \dashspace=4pt
  62. % standard setting macros
  63. \def\darkershade{%
  64.   \multiply\shadespace by 5
  65.   \divide\shadespace by 6}
  66. \def\lightershade{%
  67.   \multiply\shadespace by 6
  68.   \divide\shadespace by 5}
  69. \def\dashlineset{%
  70.   \dashlen=4pt
  71.   \dashspace=4pt}
  72. \def\dotlineset{%
  73.   \dashlen=1pt
  74.   \dashspace=2pt}
  75. % direct output to Metafont file
  76. % only valid b/w \open- and \close- graphsfile.
  77. \def\mfcmd#1{%
  78.   \immediate\write\@outx{#1}}
  79. % pen size setting
  80. \def\@pickup{%
  81.   \mfcmd{pickup pencircle scaled penwd;}}
  82. \def\pen#1{%
  83.   \mfcmd{interim penwd:=#1;}
  84.   \@pickup}
  85. % arrowhead shape setting
  86. \def\headshape#1#2{%
  87.   \mfcmd{interim hdwdr:=#1; 
  88.          interim hdten:=#2;}}
  89. % open and close file
  90. \def\opengraphsfile#1%
  91. {%
  92.   \@code33 % first graphic is `!'
  93.   \immediate\openout\@outx=#1.mf
  94.   \batchmode
  95.   \font\@graph=#1
  96.   \errorstopmode
  97.   \immediate\write16 {}
  98.   \ifx\@graph\nullfont
  99.     \font\@graph=dummy
  100.     \immediate\write16
  101.       {MFPIC: ** Graphic's TFM file #1.tfm not found. **}
  102.     \immediate\write16
  103.       {MFPIC: ** Process #1.mf! Then reprocess this file. **}
  104.     \immediate\write16
  105.       {MFPIC: `Missing character' messages will log the graphic characters.}
  106.   \else
  107.     \immediate\write16
  108.       {MFPIC: Graphic's TFM file found.}
  109.     \immediate\write16
  110.       {MFPIC: Nevertheless, if PK font's changed, reprocess #1.mf.}
  111.     \immediate\write16
  112.       {MFPIC: If TFM's changed, reprocess #1.mf, and then this TeX file.}
  113.   \fi
  114.   \immediate\write16 {}
  115.   \mfcmd{mag:=\number\mag/1000;}
  116.   \mfcmd{input graphbase.mf;}
  117.   % set global defaults
  118.   \mfcmd{mfpicenv;}
  119.   \headshape{1}{1}
  120.   \pen{0.5 pt}%
  121. }% -- end \opengraphsfile
  122. \def\closegraphsfile
  123. {%
  124.   \mfcmd{}
  125.   \mfcmd{endmfpicenv;}
  126.   \mfcmd{end.}
  127.   \immediate\closeout\@outx
  128. }% -- end \closegraphsfile
  129. % a useful macro not in plain TeX.
  130. \def\mf@gobble#1{}
  131. % for output of #  (c/o Donald Arseneau).
  132. \def\mf@s{\expandafter\mf@gobble\string\#}
  133. % beginning mfpicture environment
  134. \def\@mfpicture#1#2#3#4#5#6{%
  135.   % dimension conversion: ##1 is a dimen.
  136.   \def\@xconv##1{%
  137.     \global\advance ##1 by -#3 \mfpicunit
  138.     \global ##1 = #1 ##1}
  139.   \def\@yconv##1{%
  140.     \global\advance ##1 by -#5 \mfpicunit
  141.     \global ##1 = -#2 ##1}
  142.   % set up TeX
  143.   \global\font\@tcurr=\fontname\font
  144.   \relax
  145.   \bgroup
  146.   \nullfont
  147.   \global\@graphwd = #4 \mfpicunit
  148.   \@xconv\@graphwd
  149.   \global\setbox\@labeledgraph=\vtop
  150.     {\hbox{\@graph\char\@code}}
  151.   % set up Metafont file
  152.   \mfcmd{}
  153.   \mfcmd{unitlen:=\the\mfpicunit\mf@s;}
  154.   \mfcmd{xscale:=#1*unitlen; yscale:=#2*unitlen;
  155.     xneg:=#3; xpos:=#4;
  156.     yneg:=#5; ypos:=#6;}
  157.   \mfcmd{beginchar (\the\@code,
  158.     (xpos-xneg)*xscale,
  159.     (ypos-yneg)*yscale,
  160.     0);}
  161.   \mfcmd{setztr;}
  162.   \@pickup
  163.   % metafont drawing macros
  164.   % points, lines, and arrows
  165.   \def\point##1{%
  166.     \mfcmd{pointd(##1, \the\pointsize);}}
  167.   \def\line##1{%
  168.     \mfcmd{line(##1);}}
  169.   \def\arrow##1{%
  170.     \mfcmd{arrow(##1, \the\headlen);}}
  171.   \def\dottedline##1{%
  172.     \mfcmd{dottedline(##1, \the\dashlen,
  173.       \the\dashspace);}}
  174.   \def\dottedarrow##1{%
  175.     \mfcmd{dottedarrow(##1, \the\dashlen,
  176.       \the\dashspace, \the\headlen);}}
  177.   % axes and axis marks
  178.   \def\axes
  179.     {\mfcmd{axes(\the\axisheadlen);}}
  180.   \def\xmarks##1{%
  181.     \mfcmd{xmarks(\the\hashlen, ##1);}}
  182.   \def\ymarks##1{%
  183.     \mfcmd{ymarks(\the\hashlen, ##1);}}
  184.   % polygons
  185.   \def\rect##1{%
  186.     \mfcmd{rect(##1);}}
  187.   \def\dottedrect##1{%
  188.     \mfcmd{dottedrect(##1,
  189.       \the\dashlen, \the\dashspace);}}
  190.   \def\block##1{%
  191.     \mfcmd{block(##1);}}
  192.   \def\rectshade##1{%
  193.     \mfcmd{rectshade(\the\shadespace, ##1);}}
  194.   \def\polygon##1{%
  195.     \mfcmd{curve(false, true, ##1);}}
  196.   \def\polyshade##1{%
  197.     \mfcmd{cycleshade(\the\shadespace,
  198.       false, ##1);}}
  199.   \def\polyfill##1{%
  200.     \mfcmd{cycleshade(0, false, ##1);}}
  201.   % circles and ellipses
  202.   \def\circle##1{%
  203.     \mfcmd{circle(##1);}}
  204.   \def\ellipse##1{%
  205.     \mfcmd{ellipse(##1, 0);}}
  206.   \def\rotatedellipse##1{%
  207.     \mfcmd{ellipse(##1);}}
  208.   \def\circshade##1{%
  209.     \mfcmd{circshade(\the\shadespace, ##1);}}
  210.   \def\ellshade##1{%
  211.     \mfcmd{ellshade(\the\shadespace, ##1, 0);}}
  212.   \def\rotatedellshade##1{%
  213.     \mfcmd{ellshade(\the\shadespace, ##1);}}
  214.   \def\cdisk##1{%
  215.     \mfcmd{circshade(0, ##1);}}
  216.   \def\edisk##1{%
  217.     \mfcmd{ellshade(0, ##1, 0);}}
  218.   \def\rotatededisk##1{%
  219.     \mfcmd{ellshade(0, ##1);}}
  220.   % circular arcs
  221.   \def\arc##1{%
  222.     \mfcmd{arc(##1);}}
  223.   \def\arcarrow##1{%
  224.     \mfcmd{arcarrow(\the\headlen, ##1);}}
  225.   \def\arcshade##1{%
  226.     \mfcmd{arcshade(\the\shadespace, ##1);}}
  227.   \def\arcfill##1{%
  228.     \mfcmd{arcshade(0, ##1);}}
  229.   % polar coordinates
  230.   \def\linedir##1{%
  231.     \mfcmd{linedir(##1);}}
  232.   \def\arrowdir##1{%
  233.     \mfcmd{arrowdir(\the\headlen, ##1);}}
  234.   \def\arcth##1{%
  235.     \mfcmd{arcth(##1);}}
  236.   \def\arctharrow##1{%
  237.     \mfcmd{arctharrow(\the\headlen, ##1);}}
  238.   \def\wedgeshade##1{%
  239.     \mfcmd{wedgeshade(\the\shadespace, ##1);}}
  240.   \def\wedgefill##1{%
  241.     \mfcmd{wedgeshade(0, ##1);}}
  242.   % curves
  243.   \def\curve##1{%
  244.     \mfcmd{curve(true, false, ##1);}}
  245.   \def\polyline##1{%
  246.     \mfcmd{curve(false, false, ##1);}}
  247.   \let\polycurve=\polyline
  248.   \def\curvedarrow##1{%
  249.     \mfcmd{curvedarrow(true,
  250.       \the\headlen, ##1);}}
  251.   \def\polyarrow##1{%
  252.     \mfcmd{curvedarrow(false,
  253.       \the\headlen, ##1);}}
  254.   % cyclic curves
  255.   \def\cyclic##1{%
  256.     \mfcmd{curve(true, true, ##1);}}
  257.   \def\cycleshade##1{%
  258.     \mfcmd{cycleshade(\the\shadespace,
  259.       true, ##1);}}
  260.   \def\cyclefill##1{%
  261.     \mfcmd{cycleshade(0, true, ##1);}}
  262.   % functions
  263.   \def\function##1{%
  264.     \mfcmd{function(true, ##1);}}
  265.   \def\polyfunction##1{%
  266.     \mfcmd{function(false, ##1);}}
  267.   \def\parafcn##1{%
  268.     \mfcmd{parafcn(true, ##1);}}
  269.   \def\polyparafcn##1{%
  270.     \mfcmd{parafcn(false, ##1);}}
  271.   \def\shadefcn##1##2##3{%
  272.     \mfcmd{shadefcn(\the\shadespace, ##1)
  273.       (##2) (##3);}}
  274.   % labels
  275.   \def\@mflabel[##1##2]##3##4##5{%
  276.     % compute width of graph with label
  277.     % and reset size if necessary
  278.     {% local env. for \@btemp.
  279.       \chardef\@btemp=255\relax
  280.       \setbox\@btemp=\hbox{\@tcurr##5}
  281.       {% local env. for \@xtemp.
  282.         \dimendef\@xtemp=0\relax
  283.         \@xtemp = ##3 \mfpicunit
  284.         \@xconv\@xtemp
  285.         \advance\@xtemp by\wd\@btemp
  286.         \ifdim\@xtemp>\@graphwd
  287.           \global\@graphwd=\@xtemp\fi
  288.       }% end local env.
  289.       % set label onto picture
  290.       \global\setbox\@labeledgraph=
  291.       \vtop{\unvbox\@labeledgraph
  292.         \vbox to 0 pt{%
  293.           \if##1t\relax\else
  294.           \if##1c\kern-0.5\ht\@btemp\else
  295.           \if##1b\kern-\ht\@btemp\else
  296.             \immediate\write16 {}
  297.             \immediate\write16
  298.               {MFPIC: label: vertical option `##1' unknown, will treat as `b'}
  299.             \immediate\write16 {}
  300.             \kern-\ht\@btemp
  301.           \fi\fi\fi
  302.           {% local env. for \@ytemp.
  303.             \dimendef\@ytemp=0\relax
  304.             \@ytemp = ##4 \mfpicunit
  305.             \@yconv\@ytemp
  306.             \kern\@ytemp
  307.           }% end local env.
  308.           \hbox{%
  309.             \if##2l\relax\else
  310.             \if##2c\kern-0.5\wd\@btemp\else
  311.             \if##2r\kern-\wd\@btemp\else
  312.               \immediate\write16 {}
  313.               \immediate\write16
  314.                 {MFPIC: label: horizontal option `##2' unknown, will treat as `l'}
  315.               \immediate\write16 {}
  316.             \fi\fi\fi
  317.             {% local env. for \@xtemp.
  318.               \dimendef\@xtemp=0\relax
  319.               \@xtemp = ##3 \mfpicunit
  320.               \@xconv\@xtemp
  321.               \kern\@xtemp
  322.             }% end local env.
  323.             \box\@btemp}% -- end \hbox
  324.           \vss}% -- end \vbox
  325.         \nointerlineskip}% -- end \vtop
  326.     }% end local env.
  327.   }% -- end \@mflabel
  328.   \def\@labeldefault{\@mflabel[bl]}
  329.   \def\@dolabel{%
  330.     \if[\@nchr\expandafter\@mflabel
  331.     \else\expandafter\@labeldefault\fi}
  332.   \def\mflabel{\futurelet\@nchr\@dolabel}
  333.   % captions
  334.   % \@docaption is called by \endmfpicture
  335.   \def\@docaption{}
  336.   \def\mfcaption##1{%
  337.     % redefine to set caption
  338.     \def\@docaption{%
  339.       % allow forced line breaks
  340.       \def\\{%
  341.         \global\font\@curr=\fontname\font
  342.         \relax\egroup \hbox\bgroup\@curr}
  343.       % compute adjustments to center
  344.       \chardef\@btemp=255\relax
  345.       \setbox\@btemp=\vbox
  346.         {\hbox{\@tcurr##1}}
  347.       \ifdim\wd\@btemp>\@graphwd
  348.       \global\@graphwd=\wd\@btemp\fi
  349.       % set caption onto picture
  350.       \global\setbox\@labeledgraph=\vbox
  351.       {\unvbox\@labeledgraph
  352.         \medskip\hbox
  353.         {\noindent
  354.           \dimendef\@centerskip=0\relax
  355.           \@centerskip=\@graphwd
  356.           \advance\@centerskip -\wd\@btemp
  357.           \divide\@centerskip by 2\relax
  358.           \kern\@centerskip
  359.           \box\@btemp
  360.         }% -- end \hbox
  361.         \vss
  362.       }% -- end \vbox
  363.     }% -- end \@docaption
  364.   }% -- end \mfcaption
  365.   % backward compatibility:
  366.   \let\label=\mflabel
  367.   \let\caption=\mfcaption
  368.   % end of object macros
  369. }% -- end \@mfpicture
  370. % setting up optional arguments
  371. \def\@piciii#1#2#3#4{%
  372.   \@mfpicture{\@param}{\@param}%
  373.     {#1}{#2}{#3}{#4}}
  374. \def\@picii[#1]#2#3#4#5{%
  375.   \@mfpicture{\@param}{#1}%
  376.     {#2}{#3}{#4}{#5}}
  377. \def\@pici{%
  378.   \if[\@nchr\expandafter\@picii
  379.   \else\expandafter\@piciii\fi}
  380. \def\mfpicture [#1]{%
  381.   {% local env. for \@outrange.
  382.     \def\@outrange {%
  383.       \immediate\write16 {}
  384.       \immediate\write16
  385.     {MFPIC: ** Sorry, mfpic makes characters between 0 and 255, inclusive;}
  386.       \immediate\write16
  387.     {MFPIC: ** if you want more, then I suggest you start another graphics font;}
  388.       \immediate\write16
  389.     {MFPIC: * in the meantime mfpic will use the dummy font for new graphics.}
  390.       \immediate\write16 {}
  391.       \font\@graph=dummy\relax
  392.     }% -- end \@outrange
  393.     \ifx \@code <  0 \@outrange \else
  394.     \ifx \@code > 255 \@outrange \fi\fi
  395.   }% end local env.
  396.   \def\@param{#1}%
  397.   \futurelet\@nchr\@pici
  398. }% -- end \mfpicture
  399. % ending mfpicture environment:
  400. \def\endmfpicture{%
  401.   \mfcmd{endchar;}
  402.   \@docaption\egroup
  403.   \hbox to\@graphwd{%
  404.     \box\@labeledgraph\hss}
  405.   \global\advance\@code1
  406. }% -- end \endmfpicture
  407. %
  408. % Abbreviations:
  409. \let\mfpic=\mfpicture
  410. \let\endmfpic=\endmfpicture
  411. %
  412. \if@inlatex
  413.   \relax
  414. \else % NOT in LaTeX.
  415.   % backward compatibility:
  416.   \let\picture=\mfpicture
  417.   \let\endpicture=\endmfpicture
  418. \fi
  419. % restore catcode.
  420. \catcode`@=\oldatcatcode
  421. %
  422. % end mfpic.tex
  423.